package transclusion.items;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import processing.core.PApplet;

import wordcram.*;
import transclusion.DAG;

public class Article implements Itemable {
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Article other = (Article) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	DAG dag;
	String name;
	Article source;
	ArrayList<Word> alphabet;
	long priority;
	
	public Article(DAG d, String name) {
		this(d, name, null, 0);
	}
	
	public Article(DAG d, String name, Article source) {
		this(d, name, source, 0);
	}
	
	public Article(DAG d, String name, long p) {
		this(d, name, null, p);
	}
	
	public Article(DAG d, String name, Article source, long p) {
		this.dag = d;
		this.name = name;
		this.source = source;
		this.priority = p;
	}	

	public String name() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;		
	}
	
	public Itemable parent() {
		return source;
	}
	
	public void setParent(Itemable p) {
		this.source = (Article) p;		
	}
	
	public ArrayList<?> children() {
		return dag.transcludedArticles.get(this);
	}	
	
	public ArrayList<Itemable> list() {
		return (ArrayList<Itemable>) children();
	}
	
	public ArrayList<Itemable> orderedList() {
		ArrayList<Itemable> result = list();
		if(result != null) {
			Collections.sort(result, new Comparator<Itemable>() {
				public int compare(Itemable o1, Itemable o2) {
					String s1 = o1.name();
					String s2 = o2.name();
					return s1.compareTo(s2);
				} 
			});			
		}
		return result;
	}
	
	public ArrayList<Word> words() {
		ArrayList<Word> result = new ArrayList<Word>();		
		result.add((new Word(this.name(), 26)).setProperty("isSource", true).setAngle(PApplet.radians(0)));
		ArrayList<Article> childrenList = (ArrayList<Article>) children();
		if (childrenList != null)
			for( Article article : childrenList )
				//result.add(new Word(article.name(), 16).setProperty("isSource", false));
				result.add(new Word(article.name(), 16).setProperty("isSource", false).setAngle(PApplet.radians(0)));
		return result;
	}
	
	public Word[] wordsArray() {
		int size = 1;
		ArrayList<Article> childrenList = (ArrayList<Article>) children();
		if (childrenList != null)
			size = size + childrenList.size();
		Word[] result = new Word[size];
		Word source = new Word(this.name(), 26).setProperty("isSource", true).setAngle(PApplet.radians(0));
		source.setProperty("isSource", true);
		result[0] = source;
		for(int i = 1; i < size; i++) 
			//result[i] = new Word(childrenList.get(i-1).name(), 16).setProperty("isSource", false);
			result[i] = new Word(childrenList.get(i-1).name(), 16).setProperty("isSource", false).setAngle(PApplet.radians(0));
		return result;
	}
	
	public long length() {
		return dag.transcludedArticles.get(this).size();		
	}

	public long priority() {
		return priority;
	}
	
	public boolean isArticle() {		
		return true;
	}

	public boolean isCategory() {		
		return false;
	}	
	
	// Own methods:
	
	public void setPriority(long p) {
		priority = p;
	}
	
	public boolean isTarget() {
		if(source != null)
			return true;
		return false;
	}
	
	public boolean isSource() {
		return !isTarget();
	}	
}
